package com.zhou.jiancai.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.zhou.jiancai.R;
import com.zhou.jiancai.Utils.ImageLoaderUtils;
import com.zhou.jiancai.Utils.LogUtils;
import com.zhou.jiancai.Utils.ToastUtils;
import com.zhou.jiancai.activity.GifActivity;
import com.zhou.jiancai.activity.LocationAndSensorActivity;
import com.zhou.jiancai.activity.PickContactActivity;
import com.zhou.jiancai.activity.SwitchActivity;
import com.zhou.jiancai.activity.VlayoutActivity;
import com.zhou.jiancai.ui.WaveView;

import java.util.ArrayList;

import static com.zhou.jiancai.R.id.position;

/**
 * Created by Administrator on 2016/12/28.
 */

public class FriendFragment extends Fragment implements View.OnClickListener ,OnChartValueSelectedListener{

    private PieChart mChart;
    private Typeface tf;
    private ImageView img_logo;
    private WaveView waveView;
    private LinearLayout ll_friend_zone;
    private LinearLayout ll_daynight_toggle;
    private LinearLayout ll_daynight_about;
    private LinearLayout ll_mymovie;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_friend,null);
        tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");
        mChart = (PieChart)view.findViewById(R.id.pieChart1);

        //头像
        img_logo = (ImageView)view.findViewById(R.id.img_logo);
        //可以自动波浪的view
        waveView = (WaveView)view.findViewById(R.id.wave_view);
        initwaveview();

        Initlinnerlayout(  view);//可点击的linnearlayout

        mChart.getDescription().setEnabled(false);

        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");

        mChart.setCenterTextTypeface(tf);
        mChart.setCenterText(generateCenterText());
        mChart.setCenterTextSize(10f);
        mChart.setCenterTextTypeface(tf);

// radius of the center hole in percent of maximum radius
        mChart.setHoleRadius(45f);
        mChart.setTransparentCircleRadius(50f);
        //设置子事件的监听
        mChart.setOnChartValueSelectedListener(this);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setEnabled(true);


        mChart.setData(generatePieData());
        return view;

    }

    private void Initlinnerlayout(View view) {
        //可点击的linnearlayout
        ll_friend_zone = (LinearLayout)view.findViewById(R.id.ll_friend_zone);
        ll_friend_zone.setOnClickListener(this);

        ll_daynight_toggle = (LinearLayout)view.findViewById(R.id.ll_daynight_toggle);
        ll_daynight_toggle.setOnClickListener(this);
        ll_daynight_about = (LinearLayout)view.findViewById(R.id.ll_daynight_about);
        ll_daynight_about.setOnClickListener(this);
        ll_mymovie = (LinearLayout)view.findViewById(R.id.ll_mymovie);
        ll_mymovie.setOnClickListener(this);
    }

    private void initwaveview() {
        //设置头像跟着波浪背景浮动
        ImageLoaderUtils.displayRound(getContext(),img_logo,R.drawable.basketball);
        final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-2,-2);
        lp.gravity = Gravity.CENTER_VERTICAL;
        waveView.setOnWaveAnimationListener(new WaveView.OnWaveAnimationListener() {
            @Override
            public void OnWaveAnimation(float y) {
                lp.setMargins(0,0,0,(int)y+2);
                img_logo.setLayoutParams(lp);
            }
        });
    }

    private PieData generatePieData() {
        int count =7;

        ArrayList<PieEntry> entries1 = new ArrayList<PieEntry>();

        for(int i = 0; i < count; i++) {
           // entries1.add(new PieEntry((float) ((Math.random() * 60) + 40), "Quarter " + (i+1)));

           entries1.add(new PieEntry(i,"星期"));
        }

        PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2015");
        ds1.setColors(ColorTemplate.VORDIPLOM_COLORS);
        ds1.setSliceSpace(2f);
        ds1.setValueTextColor(Color.WHITE);
        ds1.setValueTextSize(12f);


        PieData d = new PieData(ds1);

        d.setValueTypeface(tf);

        return d;
    }

    private CharSequence generateCenterText() {
        SpannableString s = new SpannableString("Revenues\nQuarters 2015");
        s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0);
        return s;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_friend_zone:

                Toast.makeText(getActivity(),"点击了动态",Toast.LENGTH_SHORT).show();

                break;
            case R.id.ll_daynight_toggle:
                Toast.makeText(getActivity(),"点击了昼夜切换",Toast.LENGTH_SHORT).show();

                break;
            case R.id.ll_daynight_about:
                Toast.makeText(getActivity(),"点击了关于",Toast.LENGTH_SHORT).show();


                break;
            case R.id.ll_mymovie:
                Toast.makeText(getActivity(),"点击了我的电影",Toast.LENGTH_SHORT).show();

                break;
        }


    }

    @Override
    public void onValueSelected(Entry e, Highlight h) {

        LogUtils.i("VAL SELECTED",
                "Value: " + e.getY() + ", index: " + h.getX()
                        + ", DataSet index: " + h.getDataSetIndex());
   //     VAL SELECTED: Value: 5.0, index: 5.0, DataSet index: 0
     // SELECTED: Value: 6.0, index: 6.0, DataSet index: 0
        Intent i;
       if (e.getY()==0){


       }else if (e.getY()==1){
           i=new Intent(getActivity(),GifActivity.class);
           startActivity(i);

       }else if (e.getY()==2){
           i=new Intent(getActivity(),SwitchActivity.class);
           startActivity(i);
       }else if (e.getY()==3){
           Toast.makeText(getActivity(), "vlayout布局 " + position, Toast.LENGTH_SHORT).show();

           i=new Intent(getActivity(),VlayoutActivity.class);
           startActivity(i);

       }else if (e.getY()==4){
           i=new Intent(getActivity(),PickContactActivity.class);
           startActivity(i);

       }else if (e.getY()==5){
         //  i=new Intent(getActivity(),StepViewActivity.class);
         //  startActivity(i);

       }else if (e.getY()==6){

           i=new Intent(getActivity(),LocationAndSensorActivity.class);
           startActivity(i);
       }else{

       }

    }

    @Override
    public void onNothingSelected() {

    }
}
